import  numpy  as  np
import  cv2
import matplotlib.pyplot as plt

from method import pretreatment_done as pr

cap = cv2.VideoCapture('video/VID_20180207_171136.mp4')
list = []
i=1
j=1
list1=[]

while (cap.isOpened()):
    red = 0

    ret,frame = cap.read()
    if ret == False :
        break

    hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)

    if i == 1 :
        height , width =frame.shape[:2]



    # if i%5 != 0:
    #     #每5帧取一次    手机摄像头为30fps  这里保证6HZ的频率取点  可以保证180/min 的心跳 满足检测要求
    #     #也就是每个样本点 间隔 1/6 秒   这样也避免了样本过多 导致的误差
    #     i+=1
    #     continue

    #每 50*50的像素空间取一个点  节省效率   1980*1080 为例  50*50空间样本800个   10*10空间样本2W+ 1*1空间（大概率跑死）  200W+   图像几乎不会导致误差
    for h in range(0,height,50):
        for w in range(0,width,50):
            red+=frame[h][w][2]

    list.append( red )
    list1.append( {'no':i,'red':red})

    dt = 1
    t = np.arange(0, j, dt)
    nse = list
    r = np.exp(0 / 100)


    i+=1
    j+=1
    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break



cv2.destroyAllWindows()
cap.release()

result = pr.pretreatment_done(list1)

i = 0
num = len(result)-1
sum=0
while i < len(result)-1:
    sum += (result[i+1]-result[i])
    i+=1

hz = sum / num
pu = int(1800 / hz)
print(pu)
